package edu.cuit.zhuyimeng.ticket.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import edu.cuit.zhuyimeng.framework.common.result.CommonResult;
import edu.cuit.zhuyimeng.ticket.service.IFlightService;
import edu.cuit.zhuyimeng.ticket.service.ITicketDispatchService;
import edu.cuit.zhuyimeng.ticket.service.ITicketSessionManager;
import edu.cuit.zhuyimeng.ticket.service.IUserTicketManager;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 飞机票管理相关
 */
@RestController
@RequestMapping("/manage")
@RequiredArgsConstructor
@Tag(name = "飞机票管理相关")
public class TicketManageController {

    private final ITicketDispatchService ticketDispatchService;
    private final IFlightService flightService;
    private final IUserTicketManager userTicketManager;

    /**
     * 开放航班订票
     */
    @PutMapping("/")
    @Operation(summary = "开放航班订票",security = @SecurityRequirement(name = "Authorization"))
    public CommonResult<Void> openFlightBooking(@RequestParam("flightId") @NotNull Long flightId) {
        ticketDispatchService.openTicketDispatch(flightId);
        return CommonResult.success();
    }

    /**
     * 关闭航班订票
     */
    @DeleteMapping("/")
    @Operation(summary = "关闭航班订票",security = @SecurityRequirement(name = "Authorization"))
    public CommonResult<Void> closeFlightBooking(@RequestParam("flightId") @NotNull Long flightId) {
        ticketDispatchService.closeTicketDispatch(flightId);
        return CommonResult.success();
    }

    /**
     * 删除机票
     * @return 删除结果
     */
    @DeleteMapping("/ticket")
    @SaCheckLogin
    @Operation(summary = "删除机票",security = @SecurityRequirement(name = "Authorization"))
    public CommonResult<Void> deleteTicket(@RequestParam("flightId") @NotNull Long flightId) {
        userTicketManager.removeFlightTicket(flightId);
        return CommonResult.success();
    }

    /**
     * 刷新缓存（航班信息改变）
     */
    @PostMapping("/cache")
    @Operation(summary = "刷新缓存（航班信息改变）",security = @SecurityRequirement(name = "Authorization"))
    public CommonResult<Void> refreshCache(@RequestParam("flightId") @NotNull Long flightId) {
        flightService.refreshFlightCache(flightId);
        return CommonResult.success();
    }

}
